#include "Date.h"
#include "Master.h"
#include "Widget.h"

Date::Date()
{
    this->m_timer=new QTimer(this);
    connect(this->m_timer,SIGNAL(timeout()),this,SLOT(ANewDay()));
}

Date::~Date()
{
    if(NULL!=this->m_timer)
    {
        delete this->m_timer;
        this->m_timer=NULL;
    }
}

bool Date::Init()
{
    this->m_date.setDate(400,1,1);
    return true;
}

void Date::start()
{
    this->m_timer->start(5000);
    Master *master=Master::getMaster();
    master->getWidget()->updateDateStatus(this->toString());
    emit dateSig_newDay(this);
    emit dateSig_firstDayOfTen(this);
}

void Date::ANewDay()
{
    this->m_date=this->m_date.addDays(1);
    Master *master=Master::getMaster();
    master->getWidget()->updateDateStatus(this->toString());
    emit dateSig_newDay(this);

    if(this->isFirstOfTen())
        emit dateSig_firstDayOfTen(this);

    if(this->isLastDayOfMon())
        emit dateSig_LastDayOfMon(this);
}

bool Date::isLastDayOfMon()
{
    if(this->m_date.day()==this->m_date.daysInMonth())
        return true;
    return false;
}

bool Date::isFirstOfTen()
{
    if(this->m_date.day()%10==1)
        return true;
    return false;
}

QString Date::toString() const
{
    return this->m_date.toString("yyyy-MM-dd");
}

QDate Date::getQDate() const
{
    return this->m_date;
}
